package com.mf.web.basic;

import com.mf.advice.BusinessException;
import com.mf.generator.domain.basic.mapping.DataTypeMapping;
import com.mf.generator.service.basic.DataTypeMappingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据类型映射Controller
 * User: zhaoming
 * DateTime: 2015-09-08
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/web/dataTypeMapping")
public class DataTypeMappingController {

    public Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private DataTypeMappingService dataTypeMappingService;

    /**
     * 初始化
     * @param request 请求对象
     * @param response 响应对象
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("basic/mapping/data_type_mapping_index");
        return modelAndView;
    }

    /**
     * 根据条件查询数据类型映射列表
     * @return 数据类型映射列表
     */
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> selectAll() {

        List<DataTypeMapping> dataTypeMappingList = dataTypeMappingService.selectAll();

        Map<String, Object> result = new HashMap<>();
        result.put("root", dataTypeMappingList);
        return result;
    }

    /**
     * 数据类型映射更新
     * @param request 请求对象
     * @param dataTypeMapping 数据类型映射Domain
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean update(HttpServletRequest request, DataTypeMapping dataTypeMapping) {
        boolean flag = dataTypeMappingService.update(dataTypeMapping);
        if(!flag){
            throw new BusinessException("维护数据类型映射信息失败");
        }
        return flag;
    }

}